package com.briup.Config.Impl;

import com.briup.Client.Client;
import com.briup.Client.gather.Gather;
import com.briup.Config.BeanAware;
import com.briup.Config.PropertiesAware;
import com.briup.Config.SmartConfig;
import com.briup.Logger.Log;
import com.briup.Server.Server;
import com.briup.Server.Store.DBStore;
import com.briup.backUp.Backup;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.util.*;

public class SmartConfigImpl implements SmartConfig {
    //存放所有的模块对象  key:模块名  value:模块的对象
    private static Map<String,Object> beanMap = new HashMap<>();
    //存放所有属性 key:属性名    value：属性值
    private static Map<String,Object> propertiesMap = new HashMap<>();


    private static final SmartConfigImpl config;

    static {
        //静态代码块赋值
        config = new SmartConfigImpl();

        /**
         * 解析config.xml文件
         */
        //配置文件的加载
        SAXReader SAXReader = new SAXReader();
        try {
            //读取配置文件
            Document document = SAXReader.read("D:\\training2\\idea_code\\Smart\\src\\main\\resources\\config.xml");
            //获取根节点
            Element rootElement = document.getRootElement();
            for (Element element : rootElement.elements()) {
                Attribute attribute = element.attribute("class");
                //属性不为空
                if(Objects.nonNull(attribute)){
                    //获取属性值
                    String className = attribute.getValue();
                    //实例化对象
                    Object object=Class.forName(className).newInstance();
                    beanMap.put(element.getName(), object);
                }
                //获取二级标签
                List<Element> elements2 = element.elements();
                //判断一级标签下是否有对应的二级标签
                if(Objects.nonNull(elements2)&&elements2.size()>0){
                    for(Element element2 : elements2){
                        //获取标签值
                        propertiesMap.put(element2.getName(), element2.getText());
                    }
                }
            }
            //在BeanMap中拿到所有的对象
            Collection<Object> objects = beanMap.values();
            for(Object object : objects){
                //如果是PropertiesAware子类，就一定要重写方法
                if(object instanceof PropertiesAware){
                    ((PropertiesAware) object ).setProperties();
                }
                if(object instanceof BeanAware){
                    ((BeanAware)object).setBean(config);
                }
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    //构造器私有化
    private SmartConfigImpl(){
    }
    //提供公开的访问权限
    public static SmartConfigImpl getInstance(){
        return config;
    }

    //静态的集合返回方法
    public static Map<String, Object> getBeanMap() {
        return beanMap;
    }
    public static Map<String, Object> getPropertiesMap() {
        return propertiesMap;
    }

    @Override
    public Gather getGather() {
        return (Gather) (Objects.isNull(beanMap.get("gather"))?null: (Gather)beanMap.get("gather"));
    }

    @Override
    public Client getClient() {
        return (Client) (Objects.isNull(beanMap.get("client"))?null: (Client)beanMap.get("client"));
    }

    @Override
    public Server getServer() {
        return (Server) (Objects.isNull(beanMap.get("server"))?null: (Server)beanMap.get("server"));
    }

    @Override
    public DBStore getDBStore() {
        return (DBStore) (Objects.isNull(beanMap.get("dbStore"))?null: (DBStore)beanMap.get("dbStore"));
    }

    @Override
    public Log getLogger() {
        return (Log) (Objects.isNull(beanMap.get("logger"))?null: (Log)beanMap.get("logger"));
    }

    @Override
    public Backup getBackup() {
        return (Backup) (Objects.isNull(beanMap.get("backup"))?null: (Backup)beanMap.get("backup"));
    }
}
